package com.hzjiteng.newsbymvp.images;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzjiteng.newsbymvp.R;
import com.hzjiteng.newsbymvp.bean.ImageBean;
import com.hzjiteng.newsbymvp.utils.ToolsUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * 图片页的适配器
 *
 * @author Administrator
 * @created_time 2016072710:26
 */
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.MyViewHolder> {

    private List<ImageBean> list;
    private Context mContext;

    private int MAXHEIGHT;
    private int MAXWIDTH;

    public ImageAdapter(List<ImageBean> list, Context mContext) {
        this.list = list;
        this.mContext = mContext;
        MAXHEIGHT = ToolsUtil.getHeightInPx(mContext) - ToolsUtil.getStatusHeight(mContext) - ToolsUtil.dip2px(mContext, 96);
        MAXWIDTH = ToolsUtil.getWidthInPx(mContext)-20;

    }

    public void setList(List<ImageBean> list){
        if(list!=null)
            this.list = list;
        notifyDataSetChanged();
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_image_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        ImageBean bean = list.get(position);
        if (bean == null) {
            return;
        }

        holder.tv_iamgeTitle.setText(bean.getTitle());
        holder.iv_iamge.setLayoutParams(new LinearLayout.LayoutParams(MAXWIDTH,Integer.valueOf(bean.getHeight())));
        ImageLoader.getInstance().displayImage(bean.getThumburl(),holder.iv_iamge);


    }

    @Override
    public int getItemCount() {
        if (list==null)
            return 0;
        return list.size();
    }

    final class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_iamgeTitle;
        ImageView iv_iamge;

        public MyViewHolder(View itemView) {
            super(itemView);

            tv_iamgeTitle = (TextView) itemView.findViewById(R.id.tv_imageTitle);
            iv_iamge = (ImageView) itemView.findViewById(R.id.iv_iamge);
        }
    }
}
